package com.mapforge.map.tools.selection;

import java.awt.Point;
import java.awt.Rectangle;

import com.mapforge.gui.tools.SelectedTiles;
import com.mapforge.selection.ContinuousSelection;
import com.mapforge.selection.Selection;

/** */
public class RectangleSelectionTool extends AbstractSelectionTool {

	public RectangleSelectionTool() {
		super("Rectangular selection");
	}

	@Override
	public Selection performSelection(Point startPoint, Point currentPoint) {
		int x, y, w, h;
		if (startPoint.x >= currentPoint.x) {
			x = currentPoint.x;
			w = startPoint.x - x;
		} else {
			x = startPoint.x;
			w = currentPoint.x - x;
		}
		if (startPoint.y >= currentPoint.y) {
			y = currentPoint.y;
			h = startPoint.y - y;
		} else {
			y = startPoint.y;
			h = currentPoint.y - y;
		}
		Rectangle r = new Rectangle(x, y, w + 1, h + 1);
		return new ContinuousSelection(r);
	}

	@Override
	public void performEdit() {
		// intentionally left blank
	}

	@Override
	public void endEdit() {
		this.makeSelection();
		SelectedTiles tiles = SelectedTiles.getHandle();
		tiles.clearSelection();
		tiles.add(this.getCurrentSelection());
		super.endEdit();
	}
}